package org.dentalcard.ui.schedule;

import org.dentalcard.entity.DecUser;
import org.dentalcard.entity.Shift;
import org.dentalcard.entity.WorkDay;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.service.impl.DecUserDaoImpl;
import org.dentalcard.service.impl.ScheduleDaoImpl;
import org.dentalcard.ui.api.DecInternalFrame;
import org.dentalcard.ui.i18n.I18nManager;
import org.dentalcard.ui.main.MainFrameBuilder;
import org.dentalcard.ui.schedule.tools.ShiftTableModel;
import org.dentalcard.ui.schedule.tools.WorkDayModel;
import org.dentalcard.ui.schedule.tools.WorkScheduleBuilder;
import org.dentalcard.ui.schedule.tools.WorkScheduleModel;

import javax.swing.*;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 21.12.11
 * @time: 9:11
 */
public class WorkScheduleScr extends DecInternalFrame {

    private final JPanel main;
    private WorkDayModel workDayModel;
    private JTable workDay;
    private JTable workScheduleTable;

    
    public WorkScheduleScr() {
        super("Work Schedule");
        main = new JPanel(new GridBagLayout());
        this.getContentPane().add(new JScrollPane(main));
        init();
    }

    private void init() {
        initToolBar();
        int rowN = 0;

        ScheduleDaoImpl scheduleDao = new ScheduleDaoImpl();
        DecUser user = MainFrameBuilder.getUser();
        main.add(new JLabel(user.getName()), new GridBagConstraints(0, rowN++, 1, 1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(20, 5, 0, 5), 0, 0));


        WorkScheduleBuilder builder = new WorkScheduleBuilder(scheduleDao.getWorkDayList(user));

        workScheduleTable = builder.buildTable();

        main.add(workScheduleTable.getTableHeader(), new GridBagConstraints(0, rowN++, 1, 1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        main.add(workScheduleTable, new GridBagConstraints(0, rowN++, 1, 1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));

        main.add(new JLabel(), new GridBagConstraints(0, rowN++, 1, 1, 1, 1, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));


        workDayModel = new WorkDayModel();
        workDay = new JTable(workDayModel);
        createWorkDayPopupMenu();
        main.add(workDay.getTableHeader(), new GridBagConstraints(0, rowN++, 1, 1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        main.add(workDay, new GridBagConstraints(0, rowN++, 1, 1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));

    }

    private void initToolBar() {
        JMenuBar menu = new JMenuBar();
        JButton shifts = new JButton(I18nManager.getRes("button.shifts"));
        shifts.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ShiftsPopup popup = new ShiftsPopup(MainFrameBuilder.getMainFrame());
                popup.setVisible(true);
            }
        });
        menu.add(shifts);
        JButton shiftsDay = new JButton(I18nManager.getRes("button.shifts.day"));
        shiftsDay.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                WorkDayShiftManagement popup = new WorkDayShiftManagement(MainFrameBuilder.getMainFrame());
                popup.setVisible(true);
            }
        });
        menu.add(shiftsDay);

        this.getRootPane().setJMenuBar(menu);
    }

    private void createWorkDayPopupMenu() {
        final JPopupMenu shitTableMenu = new JPopupMenu("menu");
        JMenuItem newMenu = new JMenuItem("New");
        newMenu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddNewWorkDayPopup(MainFrameBuilder.getMainFrame()).setVisible(true);
                workDayModel.reload();

            }
        });
        shitTableMenu.add(newMenu);
        JMenuItem delete = new JMenuItem("Delete");
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteWorkDay();
            }
        });
        shitTableMenu.add(delete);

        workDay.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    shitTableMenu.show((Component) e.getSource(), e.getX(), e.getY());
                }
            }
        });


    }

    private void deleteWorkDay() {
        int selectedRow = workDay.getSelectedRow();
        if(selectedRow < 0){
            JOptionPane.showMessageDialog(this, I18nManager.getRes("please.select.a.row.to.delete"));
            return;
        }

        WorkDay day = workDayModel.getWorkDay(selectedRow);
        if(day == null){
            return;
        }
        if (JOptionPane.showConfirmDialog(this, "Are you sure to delete " + day.getDay() + " workDay?") == JOptionPane.YES_OPTION) {
            PersistenceManager.delete(day);
            workDayModel.reload();
        }

    }

}
